﻿using Dapper;
using Entity;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Realize;
using System.Data;
using System.Threading.Tasks;



namespace WebApplication9.Controllers.ClientSet
{
    public class JGSZIController : Controller
    {
        private readonly IConfiguration configuration;

        public JGSZIController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        public IActionResult Index()
        {
            ViewBag.s = "JGSZIController.Index";
            return View(); // 若视图不需要模型数据，可直接返回空视图
        }
        public async Task<IActionResult> Index2(int pageSize,int currentPage)
        {
            //CFFK fFK = new CFFK();
            //List<Config_File_First_Kind> zhi=await fFK.SelectCFFKAsync(configuration["SqlServer"]);
            //return Ok(new
            //{
            //    list = zhi
            //});
            string sqlserver = configuration["SqlServer"];
            using (SqlConnection conn = new SqlConnection(sqlserver))
            {
                //@pageSize, @tableName, @keyName, @currentPage, @rows
                //分页存储过程
                DynamicParameters param = new DynamicParameters();
                param.Add("@pageSize", pageSize);
                param.Add("@keyName", "ffk_id");
                param.Add("@tableName", "config_file_first_kind");
                param.Add("@currentPage", currentPage);
                param.Add("@totalRows", direction: ParameterDirection.Output, dbType: DbType.Int32);
                string sql = "exec [dbo].[proc_FenYedd] @pageSize, @keyName, @tableName, @currentPage, @totalRows out";
                List<Config_File_First_Kind> list = conn.Query<Config_File_First_Kind>(sql, param).ToList();

                FenYe<Config_File_First_Kind> fenYecs = new FenYe<Config_File_First_Kind>();
                fenYecs.List = list;
                fenYecs.Rows = param.Get<int>("totalRows");
                //return fenYecs;
                return Json(fenYecs);

            }

        }

        public IActionResult AddCFFK()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> AddCFFK(Config_File_First_Kind hd)
        {
            //操作
            ICFFK cFFK = new CFFK();
           int js=await cFFK.AddCFFKAsync(hd, configuration["SqlServer"]);
            if (js > 0)
            {
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

        public async Task<IActionResult> Update(int id)
        {
            ICFFK cFFK = new CFFK();
            ViewData.Model=await cFFK.SelectCFFKIDAsync(id, configuration["SqlServer"]);
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Update(Config_File_First_Kind ch,int id)
        {
            //根据参数做修改操作
            ICFFK cFFK = new CFFK();
            int result = await cFFK.UpdateCFFKAsync(ch, configuration["SqlServer"],id);
            if (result > 0)
            {
                return RedirectToAction("Index");
            }
            else
            {
                return View();
              
            }
           
        }

        public async Task<IActionResult> Delete(int id)
        {
            ICFFK cFFK = new CFFK();
            int zhi =await cFFK.DeleteCFFKAsync(id, configuration["SqlServer"]);
            if (zhi > 0)
            {
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
           
        }

    }
}
